/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import SQL.SQL;
import Auth.Auth4Code;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public class setStatusScore {

    public static int zoombiePostRate = 100;
    public static int agencyPostRate = 20;
    public static int normalPostRate = 5;
    public static Map<String, Double> moduleInterest = new HashMap<String, Double>();

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, FileNotFoundException, IOException, WeiboException, InterruptedException {
        Auth4Code.prepareWeibo();
        DataPreparation data = new DataPreparation();
        relationScore.relationScore(data);
        getModuleInterest(data);
        getStatusScore(data);

    }

    public static void getStatusScore(DataPreparation data) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        String query = "SELECT `no`,`fid`,`score`,`reposts_count`,`comments_count`,`module` FROM `wb_demo_status` WHERE `uid`='" + Config.uid + "';";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        //建立到MySQL的连接
        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/weibonews?useUnicode=true&characterEncoding=gb2312", "root", "");
        //执行SQL语句
        Statement stmt = (Statement) conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String no = rs.getString("no");
            String fid = rs.getString("fid");
            double friendScore = 0;
            if (data.influence.containsKey(fid)) {
                friendScore = data.influence.get(fid);
            }
            String module = "普通微博";
            if (rs.getString("module") != null) {
                module = rs.getString("module");
            }
            double moduleScore = moduleInterest.get(module);
            double statusScore = 0.0;
            double repostScore = Math.log(rs.getInt("reposts_count") + 1.5);
            double commentScore = Math.log(rs.getInt("comments_count") + 1.5);
            if (module.equals("营销类微博")) {
                statusScore = friendScore * 10000000 * moduleScore * repostScore;
            } else if (module.equals("机构媒体微博")) {
                statusScore = friendScore * 10000000 * moduleScore * repostScore;
            } else {
                statusScore = friendScore * 10000000 * moduleScore * commentScore;
            }
            String v = statusScore + "";
            System.out.println(v + " " + friendScore + " " + moduleScore + " " + repostScore);
            String[] values = {v};
            String[] columns = {"score"};
            String table = "wb_demo_status";
            SQL.SQL(SQL.updateValue(columns, values, table, "no", no));
        }
    }

    public static void getModuleInterest(DataPreparation data) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {

        moduleInterest = getInterestValue(data);
        double[] peopleInterest = getPeopleInterest(data);
        double[] repostInterest = getRepostInterest();
        Set keys = moduleInterest.keySet();
        int count = keys.size();
        for (int i = 0; i < count; i++) {
            String key = (String) keys.toArray()[i];
            double v = moduleInterest.get(key);
            v *= (peopleInterest[0] * repostInterest[0] / normalPostRate);
            moduleInterest.put(key, v);
        }
        double vagency = (peopleInterest[1] * repostInterest[1] / agencyPostRate);
        double vzoombie = (peopleInterest[2] * repostInterest[2] / zoombiePostRate);
        moduleInterest.put("机构媒体微博", vagency);
        moduleInterest.put("营销类微博", vzoombie);
        moduleInterest.put("重要微博", peopleInterest[0] * repostInterest[0] / normalPostRate);
        moduleInterest.put("普通微博", peopleInterest[0] * repostInterest[0] / normalPostRate);
        String columns[]={"normal","agency","zoombie"};
        String values[]={ repostInterest[0]+"", repostInterest[1]+"", repostInterest[2]+""};
        SQL.SQL(SQL.updateValue(columns, values, "wb_demo_interest", "uid", Config.uid));
    }

    public static double[] getPeopleInterest(DataPreparation data) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        double agencyInterest = (double) data.data.agencyCount / (double) data.data.mFIDs.size();
        double zoombieInterest = (double) data.data.zoombieCount / (double) data.data.mFIDs.size();
        double normalInterest = (double) (data.data.normalCount + data.data.celebrityCount) / (double) data.data.mFIDs.size();
        double[] peopleInterest = {normalInterest, agencyInterest, zoombieInterest};
        
        return peopleInterest;

    }

    public static Map<String, Double> getInterestValue(DataPreparation data) {
        getInterest.getLargeCatInterest(data);
        getInterest.getInterestRate();
        return getInterest.interestRate;
    }

    public static double[] getRepostInterest() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        String query = "SELECT * FROM `wb_ex_blogrepost` WHERE `uid`='" + Config.uid + "';";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        //建立到MySQL的连接
        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/weibonews?useUnicode=true&characterEncoding=gb2312", "root", "");
        //执行SQL语句
        Statement stmt = (Statement) conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        int agencyRC = 0;
        if (rs.next()) {
            String v = rs.getString("agencyRC");
            agencyRC = Integer.parseInt(v);
            v = rs.getString("mediaRC");
            agencyRC += Integer.parseInt(v);
            v = rs.getString("campusRC");
            agencyRC += Integer.parseInt(v);
            v = rs.getString("websiteRC");
            agencyRC += Integer.parseInt(v);
        }
        int zoombieRC = 0;
        if (rs.getString("zoombieRC") != null) {
            zoombieRC = Integer.parseInt(rs.getString("zoombieRC"));
        }
        int normalRC = 0;
        if (rs.getString("normalRC") != null) {
            normalRC = Integer.parseInt(rs.getString("normalRC"));
        }
        int celebrityRC = 0;
        if (rs.getString("celebrityRC") != null) {
            celebrityRC = Integer.parseInt(rs.getString("celebrityRC"));
        }
        conn.close();
        double sum = (double) (normalRC + celebrityRC + agencyRC + zoombieRC);
        double[] repostInterest = {(double) normalRC / sum, (double) agencyRC / sum, (double) zoombieRC / sum};
        return repostInterest;
    }
}
